/**
 * 
 * @author jsxgr@qq.com
 * 
 */

import * as db_context from '@/models/db_context'
const Template = window.require('art-template');

const commonAction = {
    test: (path: string) => {
      return "test";
    },
    //转驼峰
    toHump: ((str: string): string => {
      const arr = str.split('_');
       return arr.map(function (item, index) {
           if (index === 0) {
               return item;
           } else {
               return item.charAt(0).toUpperCase() + item.slice(1);
           }
       }).join('');
   }),
   //重置表格
   resetTables:async (tableName:string)=>{
     console.log('resetTables')
     //得到项目信息
     const project=await db_context.Project.getById(1);
     //得到模版
     const template=await db_context.Template.getById(project.templateId);
 
     //得到开发语言
     const code=await db_context.GenCode.getById(template.codeId);

     //得到语言字段映射
     const mapping=db_context.GenCodeMapping.getListByOption({"codeId":code.id});

     //得到数据表格
     let tables=[];
     if(tableName==""){
       tables=await db_context.GenTable.getList();
     }else{
      tables=await db_context.GenTable.getListByOption({"tableName":tableName});
     }
     
     
     tables.forEach(async element => {
       let tableName:string=element.tableName;

       console.log(tableName);
       console.log(project.autoRemovePre);
       //去除表前缀
       if(project.autoRemovePre=="1"){
      
         if(tableName.startsWith(project.tablePrefix)){
          tableName=tableName.replace(project.tablePrefix,"");
         }
        
       }
       const className=commonAction.toHump(tableName);
       element.className=className;
       element.businessName=className;
       element.functionName=element.tableComment;
       element.functionAuthor=project.author;
       element.packageName=project.packageName;
       //更新表格
      await db_context.GenTable.updateItemById(element,element.id);
       //

     });
     //更新列信息
     await commonAction.resetColumn(code.id);



   },

   resetColumn:async (codeId:number)=>{
    
    //得到语言字段映射
      const mapping=db_context.GenCodeMapping.getListByOption({"codeId":codeId});

      const columns=await db_context.GenTableColumn.getList();
      columns.forEach(async element => {

        const columnType=element.columnType;//int4
        const searchArr = (await mapping).filter(item => columnType.indexOf(item.tableField)>=0);

        if(searchArr.length>0){
          element.codeType=searchArr[0].codeField;
        }else{
          element.codeType="string";
        }
        element.codeField=commonAction.toHump(element.columnName);
        await db_context.GenTableColumn.updateItemById(element,element.id);
    
        
      });
   },
   //预览代码
   preViewCode:async(table:string)=>{


    
    Template.defaults.imports.up_first = function (str:string) {
      return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
    Template.defaults.imports.low_first = function (str:string) {
      return str.substring(0, 1).toLowerCase() + str.substring(1);
    }
    Template.defaults.imports.add_fk = function (str:string) {
      return "{{"+str+"}}";
    }


    const backObj:any={};
    console.log('preViewCode')
    //得到项目信息
    const project=await db_context.Project.getById(1);
    //得到模版
    //const template=await db_context.Template.getById(project.templateId);

    //如果传一个取一个列表
    let option={};
    if(table!=""){
      option={"tableName":table}
    }
    const tables:any=await db_context.GenTable.getListByOption(option);
    let num = 0
    for(num=0;num<tables.length;num++) {
      //组装参数
      const element:any=tables[num]; 
      const data:any={};
      //project参数
      data.title=project.name;
      data.author=project.author;
      data.packageName=project.packageName;
      data.genPath=project.genPath;
      //table参数
      data.tableName=element.tableName;
      data.tableComment=element.tableComment;
      data.className=element.className;
      data.packageName=element.packageName;
      data.moduleName=element.moduleName;
      data.businessName=element.businessName;
      data.tableGenPath=element.genPath;
      //行参数

      const columns=await db_context.GenTableColumn.getListByOption({"tableId":element.id});
      data.columns=columns;
      //开始编译模版
      console.log(data);
      console.log(data);

      //取到模版信息
      const tempOption = {templateId: (project.templateId)  };
      const files = await db_context.TemplateFile.getListByOption(tempOption);

      //
      const tabs:any[]=[];
      files.forEach(elementFile => {

        //file name
        const fileName= elementFile.fileName;
        const render1=  Template.compile(fileName);
        const fileNameTo  = render1(data);
        //file content
        const fileContent = elementFile.fileContent;
        const source = fileContent;
        const render = Template.compile(source);
        const html =render(data);

        //file path
        const filepath = elementFile.genPath;
        const renderPath = Template.compile(filepath);
        const genPath=renderPath(data);

        const tab = {
            title: fileNameTo,
            name: elementFile.fileName,
            path:genPath,
            content: html
        }
        tabs.push(tab);

    });

    const tableName:string=element.tableName;
    backObj[tableName]=tabs;
    }
    console.log('render5-------------');
    return backObj;
    

   }



  };
  
  export default commonAction;
  